package com.itboye.mengwu.app.ui.aizhiwu;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.MyRunnable;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.aiwujia.YuErJingMomDetailActivity;
import com.itboye.mengwu.app.ui.mymeng.WoDeDongTaiDetail2;
import com.itboye.mengwu.data.model.AllData;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.model.TieZiBaoHanReply.GetAList;
import com.itboye.mengwu.data.presenter.TieZiPresenter;

public class ActivityQueryWodeDongTaiDetail extends BaseActivity implements
		Observer {

	private ViewPager viewPager;

	private int[] images;

	private int currentPage = 0;

	boolean isShow = false;
	TextView txt;
	LinearLayout tipsBox;
	ArrayList<String> imgUrls = new ArrayList<String>();
	String message;
	ImageView imgZan;
	private String isLike;
	GetAList aList = new GetAList();
	AllData allData;
	protected String tid;
	ImageView back;
	TieZiPresenter presenter;
	PagerAdapter adapter;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_wodedongtaidetail;
	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			setResultCode(102);
			finish();
		}
	};

	@SuppressWarnings("deprecation")
	@Override
	public void onMyCreate() {
		showProgressDialog("图片加载中", true);
		IntentFilter filter = new IntentFilter("detailfinish");
		registerReceiver(receiver, filter);
		txt = (TextView) findViewById(R.id.txt);
		// imgUrls = getIntent().getStringArrayListExtra("imgUrl");
		System.out.println("接口" + imgUrls);
		message = getIntent().getStringExtra("message");
		isLike = getIntent().getStringExtra("isLike");
		// allData=getIntent().getSerializableExtra("model");
		tid = getIntent().getStringExtra("tid");
		txt.setText("\u3000\u3000" + message == null ? "无主题" : message);
		presenter = new TieZiPresenter(this);
		presenter.zhutiXiangqing(
				SPUtils.get(this, null, SpPublicParams.UID, "") + "", tid, "3");
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		if (isLike != null) {
			if (Integer.parseInt(isLike) == 1) {
				imgZan.setImageResource(R.drawable.su);
			} else {
				imgZan.setImageResource(R.drawable.se);
			}
		}
		if (tid == null || tid == "") {
			ByAlert.alert("改帖已经被删除了...");
		} else
			findViewById(R.id.one).setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(
							ActivityQueryWodeDongTaiDetail.this,
							WoDeDongTaiDetail2.class);
					intent.putExtra("tid", tid);
					intent.putExtra("title", aList.getSubject());
					intent.putStringArrayListExtra("img", imgUrls);
					startActivity(intent);
				}
			});

		LinearLayout tipsBox = (LinearLayout) findViewById(R.id.tipsBox);
		tipsBox.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (aList != null) {
					// ByAlert.alert(aList.getTid());
					Intent intent = new Intent(
							ActivityQueryWodeDongTaiDetail.this,
							WoDeDongTaiDetail2.class);
					intent.putExtra("tid", aList.getTid());
					intent.putExtra("title", aList.getSubject());
					intent.putStringArrayListExtra("img", imgUrls);
					startActivity(intent);
				}
			}
		});

		// tips = new ImageView[5];
		//
		// for (int i = 0; i < images.length; i++) {
		//
		// ImageView img = new ImageView(this);
		//
		// img.setLayoutParams(new LayoutParams(10, 10));
		//
		// tips[i] = img;
		//
		// if (i == 0)
		//
		// {
		//
		// img.setBackgroundResource(R.drawable.lunbored);
		//
		// }
		//
		// else {
		//
		// img.setBackgroundResource(R.drawable.is_this_page_img);
		//
		// }
		//
		// LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
		// new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
		// LayoutParams.WRAP_CONTENT));
		//
		// params.leftMargin = 5;
		//
		// params.rightMargin = 5;
		//
		// tipsBox.addView(img, params);
		//
		// }

		// -----��ʼ��PagerAdapter------

		adapter = new PagerAdapter() {

			@Override
			public int getCount() {

				// TODO Auto-generated method stub

				return aList.getImg().size();

			}

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {

				// TODO Auto-generated method stub

				return arg0 == arg1;

			}

			@Override
			public void destroyItem(ViewGroup container, int position, Object o) {

				// container.removeViewAt(position);

			}

			@Override
			public Object instantiateItem(ViewGroup container,
					final int position) {
				// MyRunnable
				ImageView im = new ImageView(
						ActivityQueryWodeDongTaiDetail.this);
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ aList.getImg().get(position), im);
				im.setScaleType(ScaleType.CENTER_CROP);
				try {
					closeProgressDialog();
				} catch (Exception e) {
					// TODO: handle exception
				}
				im.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (isShow) {
							findViewById(R.id.tipsBox).setVisibility(View.GONE);
						} else {
							findViewById(R.id.tipsBox).setVisibility(
									View.VISIBLE);
						}
						isShow = !isShow;
					}
				});
				container.addView(im);

				return im;

			}

		};

		// ���ĵ�ǰtip

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(receiver);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == TieZiPresenter.XiangQingSuccess) {

				aList = (GetAList) entity.getData();
				viewPager = (ViewPager) findViewById(R.id.viewpager1);
				viewPager.setAdapter(adapter);
				viewPager.setOnPageChangeListener(new OnPageChangeListener() {

					@Override
					public void onPageScrollStateChanged(int arg0) {

						// TODO Auto-generated method stub

					}

					@Override
					public void onPageScrolled(int position,
							float positionOffset, int positionOffsetPixels) {

						// TODO Auto-generated method stub

					}

					@Override
					public void onPageSelected(int position) {

						// TODO Auto-generated method stub

						// Log.e("rf", String.valueOf(position));
						//
						// tips[currentPage].setBackgroundResource(R.drawable.lunbored);
						//
						// currentPage = position;
						//
						// tips[position]
						// .setBackgroundResource(R.drawable.is_this_page_img);

					}

				});
			}
		}
	}
}